package com.gomtel.smartdevice.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchBaseFragemnt;
import com.gomtel.smartdevice.bean.ToolsContentBean;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.global.DataClass;
import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.moudle.db.entity.AppConfigurationInfo;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfo;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.ui.activity.tools.AtrialFibrillationActivity;
import com.gomtel.smartdevice.ui.activity.tools.BdActivity;
import com.gomtel.smartdevice.ui.activity.tools.DeviceConnectionActivity;
import com.gomtel.smartdevice.ui.activity.tools.EGCActivity;
import com.gomtel.smartdevice.ui.activity.tools.HeartRateActivity;
import com.gomtel.smartdevice.ui.activity.tools.MyHistoryActivity;
import com.gomtel.smartdevice.ui.activity.tools.PedometerActivity;
import com.gomtel.smartdevice.ui.activity.tools.PulseConditionActvity;
import com.gomtel.smartdevice.ui.adapter.ToolsAdapter;
import com.gomtel.smartdevice.ui.view.ImageSlideshow;
import com.gomtel.smartdevice.utils.CommonUtils;
import com.gomtel.smartdevice.utils.LogUtil;

import java.util.ArrayList;

import butterknife.BindView;

import static com.gomtel.smartdevice.moudle.event.EventCode.PERSONAGE_UP_DATA;

/**
 * Created by Administrator on 2018/5/14 0014.
 */

public class HomeFragment extends WatchBaseFragemnt implements View.OnClickListener, ImageSlideshow.OnItemClickListener, AdapterView.OnItemClickListener {

    @BindView(R.id.slide_show)
    ImageSlideshow slide_show;
    @BindView(R.id.history)
    TextView history;
    @BindView(R.id.home_grid_view)
    GridView home_grid_view;
    @BindView(R.id.title_about)
    TextView title_about;
    @BindView(R.id.title_about_life)
    TextView title_about_life;
    @BindView(R.id.title_name)
    TextView title_name;

    private DataClass dataClass;
    private ArrayList<ToolsContentBean> toolsContentBeen = new ArrayList<>();
    private ToolsAdapter toolsAdapter;
    private String onlineStatus;
    private LoginUserInfo loginUserInfo;
    private SignsOfDataInfo signsOfDataInfo;

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.CTIONCONTENTSTATUS:
                onlineStatus = commonevent.getTemp_str();
                title_about.setText(onlineStatus);
                break;
            case EventCode.BATTERY:
                title_about_life.setText(new StringBuffer().append("电量 ：").append(DataClass.Battery).append("%"));
                break;
            case EventCode.ADAPTER_UP_DATA:
                AdapterUpdata();
                break;
            case PERSONAGE_UP_DATA:
                if (signsOfDataInfo != null && !TextUtils.isEmpty(signsOfDataInfo.getNickName())) {
                    title_name.setText(signsOfDataInfo.getNickName());
                }
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initClass() {
        dataClass = new DataClass(dataManager);
    }

    @Override
    protected void initData() {
        loginUserInfo = dataManager.queryLoginUserInfo(UserContent.UserName);
        signsOfDataInfo = dataManager.querySignsOfDataInfo(UserContent.UserName);
        toolsContentBeen.addAll(dataClass.upDataToolsContent(false));
        ArrayList<Integer> bannerList = dataClass.getBannerList();
        for (int i = 0; i < bannerList.size(); i++) {
            slide_show.addImageUrl(bannerList.get(i));
        }
    }

    @Override
    protected void initView() {
        if (loginUserInfo != null) {
            title_name.setText(loginUserInfo.getNickName());
            toolsAdapter = new ToolsAdapter(toolsContentBeen, getActivity());
            home_grid_view.setAdapter(toolsAdapter);
            slide_show.setDotSpace(12);
            slide_show.setDotSize(12);
            slide_show.setDelay(3000);
            slide_show.commit();
        }
        if (signsOfDataInfo != null && !TextUtils.isEmpty(signsOfDataInfo.getNickName())) {
            title_name.setText(signsOfDataInfo.getNickName());
        }
    }

    @Override
    protected void initListener() {
        Log.e(TAG, "initListener");
        slide_show.setOnItemClickListener(this);
        history.setOnClickListener(this);
        home_grid_view.setOnItemClickListener(this);
        title_about.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.history:
                startActivity(new Intent(getActivity(), MyHistoryActivity.class));
                break;
            case R.id.title_about:
                if (!DataClass.onlineStatus) {
                    AppConfigurationInfo appConfigurationInfo = dataManager.queryAppConfigurationInfo(UserContent.UserName);
                    if (appConfigurationInfo != null) {
                        EventBusUtil.sendEvent(new CommonEvent(EventCode.LOCAL_AREA_LINKS));
                    } else {
                        startActivity(new Intent(getActivity(), DeviceConnectionActivity.class));
                    }
                } else {
                    CommonUtils.showToast(WatchApplication.getInstance(), "已经连接");
                }
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (toolsContentBeen.get(i).getToolsType()) {
            case 0:
                //showDialog.showStepAboutDialog(getActivity());
                startActivity(new Intent(getActivity(), PedometerActivity.class));
                break;
            case 1:
                startActivity(new Intent(getActivity(), EGCActivity.class));
                break;
            case 2:
                startActivity(new Intent(getActivity(), BdActivity.class));
                break;
            case 3:
//                toastUtil.showToast("暂未开放");
                startActivity(new Intent(getActivity(), PulseConditionActvity.class));
                break;
            case 4:
                startActivity(new Intent(getActivity(), HeartRateActivity.class));
                break;
            case 5:
                //toastUtil.showToast("暂未开放");
//                startActivity(new Intent(getActivity(), DeviceSettingActivity.class));
//                startActivity(new Intent(getActivity(), LocationActivity.class));
                startActivity(new Intent(getActivity(), AtrialFibrillationActivity.class));
                break;
        }
    }

    private void AdapterUpdata() {
        toolsContentBeen.clear();
        if (onlineStatus.equals("已连接")) {
            toolsContentBeen.addAll(dataClass.upDataToolsContent(true));
        } else {
            toolsContentBeen.addAll(dataClass.upDataToolsContent(false));
        }
        LogUtil.e(TAG, "toolsContentBeen : " + toolsContentBeen.size());
        toolsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        slide_show.releaseResource();
    }
}
